package com.ruoyi.web.controller.house;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.data.MD5;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.info.domain.HouseDoorInfoT;
import com.ruoyi.info.service.IHouseDoorInfoTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 户信息修改Controller
 * 
 * @author tyq
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/house/info")
public class HouseDoorInfoTController extends BaseController
{
    @Autowired
    private IHouseDoorInfoTService houseDoorInfoTService;

    /**
     * 查询户信息修改列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseDoorInfoT houseDoorInfoT) throws Exception {
        startPage();
        if(houseDoorInfoT.getUserName()!=null){
            houseDoorInfoT.setUserName(MD5.encrypt(houseDoorInfoT.getUserName()));
        }
        List<HouseDoorInfoT> list = houseDoorInfoTService.selectHouseDoorInfoTList(houseDoorInfoT);
        for(HouseDoorInfoT houseDoorInfo:list){
            houseDoorInfo.setPapersCode(MD5.decrypt(houseDoorInfo.getPapersCode()));
            houseDoorInfo.setUserName(MD5.decrypt(houseDoorInfo.getUserName()));
            houseDoorInfo.setTel(MD5.decrypt(houseDoorInfo.getTel()));
        }
        return getDataTable(list);
    }

    /**
     * 获取户信息修改详细信息
     */
    @GetMapping(value = "/{doorId}")
    public AjaxResult getInfo(@PathVariable("doorId") Long doorId) throws Exception {
        HouseDoorInfoT houseDoorInfoT = houseDoorInfoTService.selectHouseDoorInfoTByDoorId(doorId);
        houseDoorInfoT.setPapersCode(MD5.decrypt(houseDoorInfoT.getPapersCode()));
        houseDoorInfoT.setUserName(MD5.decrypt(houseDoorInfoT.getUserName()));
        houseDoorInfoT.setTel(MD5.decrypt(houseDoorInfoT.getTel()));
        return AjaxResult.success(houseDoorInfoT);
    }

    /**
     * 修改户信息修改
     */
     @Log(title = "户信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseDoorInfoT houseDoorInfoT ) throws Exception {
        if(houseDoorInfoT.getPapersCode()!=null){
            houseDoorInfoT.setPapersCode(MD5.encrypt(houseDoorInfoT.getPapersCode()));
        }
        if(houseDoorInfoT.getUserName()!=null){
            houseDoorInfoT.setUserName(MD5.encrypt(houseDoorInfoT.getUserName()));
        }
        if(houseDoorInfoT.getTel()!=null){
            houseDoorInfoT.setTel(MD5.encrypt(houseDoorInfoT.getTel()));
        }
         houseDoorInfoT.setCreater(getUsername());
        return toAjax(houseDoorInfoTService.updateHouseDoorInfoT(houseDoorInfoT));
    }


}
